<?php
/**
 * Created by PhpStorm.
 * User: lx
 * Date: 2019/12/27
 * Time: 15:03
 */

namespace app\api\controller;

use data\service\Member;
use data\service\MemberFamily;

/**
 * 亲友卡
 *
 * Class Family
 * @package app\api\controller
 * @author dzl
 */
class Family
{

    /**
     * 亲友卡列表  经典
     *
     * @return string
     * @throws \Exception
     * @author dzl
     */
    public function getClassicFamilyCardList()
    {
        $service = new \data\service\Family();
        $condition['is_status'] = 1;
        $condition['is_classic'] = 1;
        $list = $service->getFamilyCardList($page_size = 0, $page_index = 0, $order = "", $condition);
        return return_json_data($list);
    }

    /**
     * 亲友卡列表  精选
     *
     * @return string
     * @throws \Exception
     * @author dzl
     */
    public function getSelectedFamilyCardList()
    {
        $service = new \data\service\Family();
        $condition['is_status'] = 1;
        $condition['is_classic'] = 2;
        $list = $service->getFamilyCardList($page_size = 0, $page_index = 0, $order = "", $condition);
        return return_json_data($list);
    }

    /**
     * 获取个人的亲友卡
     *
     * @return string
     * @throws \Exception
     * @author dzl
     */
    public function getPersonalFamilyCard()
    {
        $id = request()->post('id', '');
        $service = new \data\service\Family();
        $condition['member_id'] = $id;
        $condition['is_exhaust'] = 1;
        $list = $service->getPersonalList($condition, $page_size = 0, $page_index = 0, $order = "");
        return return_json_data($list);

    }

    /**
     * 获取支付详情
     *
     * @return string
     * @throws \Exception
     * @author dzl
     */
    public function getPayInfo()
    {
        $theme = request()->post('theme', '');
        $service = new \data\service\Family();
        $list = $service->getPayInfo($theme);
        return return_json_data($list);
    }

    /**
     * 购买亲友卡
     *
     * @return string
     * @throws \Exception
     * @author dzl
     */
    public function purchaseFamilyCard()
    {
        $id = request()->post('id', '');
        $order_no = request()->post('order_no', '');

        $data['id'] = $id;
        $data['order_no'] = $order_no;

        $service = new \data\service\Family();

        $result = $service->purchaseFamilyCard($data);
        return return_json_data($result);
    }

    /**
     * 唤起微信支付
     *
     * @return string
     * @throws \Exception
     * @author dzl
     */
    public function getPayWxInfo()
    {
        $id = request()->post('id', '');
        $card_id = request()->post('card_id', '');
        $data['id'] = $id;
        $data['card_id'] = $card_id;
        $service = new \data\service\Family();
        $service->updateOverOrder($id);

        $result = $service->getPayWxInfo($data, $card_id);
        return return_json_data($result);
    }

    /**
     * 我的亲友卡
     *
     * @return string
     * @throws \Exception
     * @author dzl
     */
    public function myFamily()
    {
        $id = request()->post('id', '');
        $username = request()->post('username', '');

        if (!empty($username)) {
            $condition['member_name'] = array(
                "like",
                "%" . $username . "%"
            );
        }

        $condition['parent_id'] = $id;
        $condition['is_share'] = 1;

        $service = new \data\service\Family();

        $result = $service->getMemberFamily($condition);
        return return_json_data($result);
    }

    /**
     * 亲友卡记录接口
     *
     * @return string
     * @throws \Exception
     * @author dzl
     */
    public function familyRecords()
    {
        $id = request()->post('id', '');
        $type = request()->post('type', 0);
        $service = new \data\service\Family();
        if ($type != 0) {
            $condition['type'] = $type;
        }
        $condition['member_id'] = $id;

        $result = $service->geFamilyRecords($condition);
        return return_json_data($result);
    }


    /**
     * 唤起分享接口
     *
     * @return string
     * @throws \Exception
     * @author dzl
     */
    public function getShareInfo()
    {
        $card_id = request()->post('card_id', '');
        $id = request()->post('id', '');

        $service = new \data\service\Family();
        $request['card_id'] = $card_id;
        $request['id'] = $id;

        $result = $service->shareInfo($request);

        return return_json_data($result);
//        $result =  $service->clearOverShare($request);

    }

    /**
     * 清除过期订单
     *
     * @return string
     * @throws \Exception
     * @author dzl
     */
    public function updateOrder(){
        $id = request()->post('id', '');
        $service = new \data\service\Family();

        $result = $service->updateOverOrder($id);
        return return_json_data($result);
    }

    /**
     * 处理分享接口
     *
     * @return string
     * @throws \Exception
     * @author dzl
     */
    public function handleShare(){
        $card_id = request()->post('card_id', '');
        $card_password = request()->post('card_password', '');
        $id = request()->post('id', '');
        $phone = request()->post('phone', '');

        $service = new \data\service\Family();
        $request['card_id'] = $card_id;
        $request['id'] = $id;
        $request['card_password'] = $card_password;
        $request['phone'] = $phone;

        $result = $service->handleShare($request);

        return return_json_data($result);
    }
}